package com.wu.controller;/**
 * @author sun
 * @date 2022/3/21 - 0:05
 */

import com.wu.exceptionhandler.GuliException;
import com.wu.pojo.User;
import com.wu.service.UserService;
import com.wu.utils.JwtUtils;
import com.wu.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description: TODO
 * @author
 * @date 2022/3/21 0:05
 * @version 1.0
 */
@RestController
@Api(tags="User模块")
@CrossOrigin
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    @PostMapping("/insertUser")
    @ApiOperation(value="增加User")
    public R insertUser(@RequestBody User user){
        try {
            userService.insertSelective(user);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }


    @GetMapping("/deleteUser/{id}")
    @ApiOperation(value="删除User")
    public R deleteUser(Integer id){
        try {
            userService.deleteByPrimaryKey(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @PutMapping("/updateUser")
    @ApiOperation(value="更新User")
    public R updateUser(@RequestBody User user){
        try {
            userService.updateByPrimaryKeySelective(user);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findUser")
    @ApiOperation(value="查询UserList")
    public R findUser(User user){
        try {
            List<User> list= userService.findUser(user);
            return R.ok().data("userList",list);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findUserById/{id}")
    @ApiOperation(value="通过id查询User")
    public R findUser(Integer id){
        try {
            User user= userService.selectByPrimaryKey(id);
            return R.ok().data("user",user);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "登录")
    @PostMapping("login")
    public R login(@RequestBody User user) {
        String token = userService.login(user);
        return R.ok().data("token", token);
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("auth/getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            User user = userService.selectByPrimaryKey(Integer.parseInt(memberId));
            return R.ok().data("user", user);
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20001,"error");
        }
    }

}
